#include "game_util.h"
#include <uuid/uuid.h>
#include <stdio.h>

#if CGAME_TARGET_PLATFORM == CGAME_PLATFORM_LINUX
#include <time.h>
#elif CGAME_TARGET_PLATFORM == CGAME_PLATFORM_MAC
#include <sys/time.h>
#endif



size_t __get_uuid(char * dest,BOOL sep){
  uuid_t uu;
  uuid_generate(uu);

  if(TRUE == sep)
    return sprintf(dest,"%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
      uu[0],uu[1],uu[2],uu[3],
      uu[4],uu[5],
      uu[6],uu[7],
      uu[8],uu[9],
      uu[10],uu[11],uu[12],uu[13],uu[14],uu[15]
    );
  else
    return sprintf(dest,"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
      uu[0],uu[1],uu[2],uu[3],
      uu[4],uu[5],
      uu[6],uu[7],
      uu[8],uu[9],
      uu[10],uu[11],uu[12],uu[13],uu[14],uu[15]
    );
}

size_t __get_uuid(char *dest)
{
  return __get_uuid(dest,TRUE);
}


size_t __gmtime_add(time_t add,char *dest){
  time_t now;
  now = time(NULL);

  return __gmtime_format(now + add,dest,"%a, %d %b %Y %H:%M:%S");
}

size_t __gmtime_format(char *dest){
  time_t now;
  now = time(NULL);
  return __gmtime_format(now,dest,"%a, %d %b %Y %H:%M:%S");
}

size_t __gmtime_format(char *dest ,const char *format){
  time_t now;
  now = time(NULL);
  return __gmtime_format(now,dest,format);
}

size_t __gmtime_format(time_t t,char *dest ,const char *format){
  struct tm gmt;
  /* 获取 GMT 时间 */
  gmtime_r(&t,&gmt);

  return strftime(dest,30,format,&gmt);
}

size_t __localtime_format(char *dest)
{
  time_t now;
  now = time(NULL);
  return __gmtime_format(now,dest,"%a, %d %b %Y %H:%M:%S");
}
size_t __localtime_format(char *dest ,const char *format)
{
  time_t now;
  now = time(NULL);
  return __localtime_format(now,dest,format);
}
size_t __localtime_format(time_t t,char *dest ,const char *format)
{
  struct tm gmt;
  localtime_r(&t,&gmt);
  return strftime(dest,30,format,&gmt);
}


mseconds_t __get_time_ms(){
  struct timeval time;

  gettimeofday(&time,NULL);


  return time.tv_sec * 1000 + time.tv_usec / 1000;
}
